package com.lzc.springcloud.user.resources;

import com.lzc.springcloud.user.application.representation.User;
import com.lzc.springcloud.user.domain.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

import org.springframework.security.core.session.SessionRegistry;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.security.Principal;
import java.util.List;
import java.util.MissingResourceException;

@Controller
@RequestMapping("identity")
public class UserResource {

    @GetMapping("login.html")
    public String login() {
        return "login";
    }

    @RequestMapping(value = "gologin.html", method = RequestMethod.GET)
    @ResponseBody
    public Test gologin() {
        Test test = new Test();
        test.setId("123");
        test.setName("我我我");
        return test;
    }

    @RequestMapping(value = "gologin2")
    @ResponseBody
    public Test gologin2() {
        Test test = new Test();
        test.setId("123");
        test.setName("我我我");
        return test;
    }

    @RequestMapping("gologin1")
    @ResponseBody
    public User gologin1(Principal principal) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken)principal;
        User user = (User)usernamePasswordAuthenticationToken.getPrincipal();
        return user;
    }

//    @RequestMapping("login")
//    public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam String userId, @RequestParam String password, Map<String,Object> map) {
//        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userId, password);
//        Authentication authentication= authenticationManager.authenticate(token);
//        sessionAuthenticationStrategy.onAuthentication(authentication, request, response);
//
//        SecurityContextHolder.getContext().setAuthentication(authentication);
//        User userDetails = (User) SecurityContextHolder.getContext()
//                .getAuthentication()
//                .getPrincipal();
//        List<GrantedAuthority> authorities = (List<GrantedAuthority>) SecurityContextHolder.getContext()
//                .getAuthentication().getAuthorities();
//        map.put("user", userDetails);
//        map.put("authorities", authorities);
////        map.put("SESSIONID", session.getId());
//        return "index";
//    }
}
